<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html><head><title>Programming in Lua : 16</title>
<link rel="stylesheet" type="text/css" href="pil.css">
</head>
<body>
<p class="warning">
<A HREF="contents.html"><IMG TITLE="Programming in Lua (first edition)" SRC="capa.jpg" ALT="" ALIGN="left"></A>This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.<BR>The third edition targets Lua 5.2 and is available at <A HREF="http://www.amazon.com/exec/obidos/ASIN/859037985X/theprogrammil3-20">Amazon</A> and other bookstores.<BR>By buying the book, you also help to <A HREF="../donations.html">support the Lua project</A>.
</p>
<table width="100%" class="nav">
<tr>
<td width="10%" align="left"><a href="15.5.html"><img border="0" src="left.png" alt="Previous"></a></td>
<td width="80%" align="center"><a href="contents.html"><font face="Helvetica,Arial,sanserif">
<font color="gray">Programming in </font><font color="blue"> Lua</font>
</font></a></td>
<td width="10%" align="right"><a href="16.1.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
<tr>
<td width="10%" align="left"></td>
<td width="80%" align="center"><a href="contents.html#P2">Part II. Tables and Objects</a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="contents.html#16">Chapter 16. Object-Oriented Programming</a></td>
<td width="10%" align="right"></td></tr>
</table>
<hr/>
<a name="ObjectSec"><h1>16 &ndash; Object-Oriented Programming</h1></a>

<p>

<p>

<p>A table in Lua is an object in more than one sense.
Like objects, tables have a state.
Like objects, tables have an identity (a <em>selfness</em>)
that is independent of their values;
specifically, two objects (tables) with the same value
are different objects,
whereas an object can have different values at different times,
but it is always the same object.
Like objects, tables have a life cycle that is independent of
who created them or where they were created.

<p>Objects have their own operations.
Tables also can have operations:
<pre>
    Account = {balance = 0}
    function Account.withdraw (v)
      Account.balance = Account.balance - v
    end
</pre>
This definition creates a new function and stores it in
field <code>withdraw</code> of the <code>Account</code> object.
Then, we can call it as
<pre>
    Account.withdraw(100.00)
</pre>

<p>This kind of function is almost
what we call a <em>method</em>.
However, the use of the global name <code>Account</code> inside the function
is a bad programming practice.
First, this function will work only for this particular object.
Second, even for this particular object the function will work only
as long as the object is stored in that particular global variable;
if we change the name of this object,
<code>withdraw</code> does not work any more:
<pre>
    a = Account; Account = nil
    a.withdraw(100.00)   -- ERROR!
</pre>
Such behavior violates the previous principle that objects have
independent life cycles.

<p>A more flexible approach is to operate on
the <em>receiver</em> of the operation.
For that, we would have to define our method with an extra parameter,
which tells the method on which object it has to operate.
This parameter usually has the name <em>self</em> or <em>this</em>:
<pre>
    function Account.withdraw (self, v)
      self.balance = self.balance - v
    end
</pre>
Now, when we call the method we have to specify
on which object it has to operate:
<pre>
    a1 = Account; Account = nil
    ...
    a1.withdraw(a1, 100.00)   -- OK
</pre>
With the use of a <em>self</em> parameter,
we can use the same method for many objects:
<pre>
    a2 = {balance=0, withdraw = Account.withdraw}
    ...
    a2.withdraw(a2, 260.00)
</pre>

<p>This use of a <em>self</em> parameter is a central point in any
object-oriented language.
Most OO languages have this mechanism partly hidden
from the programmer,
so that she does not have to declare this parameter
(although she still can use the name <em>self</em> or
<em>this</em> inside a method).
Lua can also hide this parameter,
using the <em>colon operator</em>.
We can rewrite the previous method definition as
<pre>
    function Account:withdraw (v)
      self.balance = self.balance - v
    end
</pre>
and the method call as
<pre>
    a:withdraw(100.00)
</pre>
The effect of the colon is to add an extra hidden parameter
in a method definition
and to add an extra argument in a method call.
The colon is only a syntactic facility,
although a convenient one;
there is nothing really new here.
We can define a function with the dot syntax and call
it with the colon syntax, or vice-versa,
as long as we handle the extra parameter correctly:
<pre>
    Account = { balance=0,
                withdraw = function (self, v)
                             self.balance = self.balance - v
                           end
              }
    
    function Account:deposit (v)
      self.balance = self.balance + v
    end
    
    Account.deposit(Account, 200.00)
    Account:withdraw(100.00)
</pre>

<p>Now our objects have an identity, a state,
and operations over this state.
They still lack a class system, inheritance, and privacy.
Let us tackle the first problem:
How can we create several objects with similar behavior?
Specifically, how can we create several accounts?

<hr/>
<table width="100%" class="nav">
<tr valign="top">
<td width="90%" align="left">
<small>
  Copyright &copy; 2003&ndash;2004 Roberto Ierusalimschy.  All rights reserved.
</small>
</td>
<td width="10%" align="right"><a href="16.1.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
</table>


</body></html>

